package com.gisuni.sddc.platformManager.dao;

import java.util.Date;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.gisuni.sddc.platformManager.model.Attachment;
import com.gisuni.sddc.platformManager.model.MessageInfo;

@IocBean
public class MessageInfoDao {
	@Inject
	private BasicDao basicDao;
	
	@Inject
	private Dao dao;
	
	public List<MessageInfo> getShowMessageInfo() {
		Cnd cnd=Cnd.where("STATUS","=","1");
		cnd.desc("UPTIME");
		return basicDao.search(MessageInfo.class, cnd);
	}

	public boolean addMessageInfo(MessageInfo messageInfo) {
		MessageInfo mess=basicDao.save(messageInfo);
		if(mess!=null){
			return true;
		}
		return false;
	}

	public MessageInfo getMessageById(String id) {
		Cnd cnd=Cnd.where("id","=",id);
		MessageInfo messageInfo=basicDao.findByCondition(MessageInfo.class,cnd);
		Cnd cnd1=Cnd.where("OWNER_ID","=",id);
		cnd1.and("OWNER_FLAG","=","Message");
		List<Attachment> attachments=basicDao.search(Attachment.class, cnd1);
		if(messageInfo!=null){
			messageInfo.setAttachments(attachments);
		}
		return messageInfo;
		
	}

	public boolean updateMessageById(String id, String title, String content) {
		Cnd cnd=Cnd.where("Id","=",id);
		MessageInfo messageInfo=basicDao.findByCondition(MessageInfo.class, cnd);
		messageInfo.setId(id);
		messageInfo.setTitle(title);
		messageInfo.setContent(content);
		messageInfo.setUptime(new Date());
		return basicDao.update(messageInfo);
	}

	public List<MessageInfo> getAllMessageInfo2() {
		Cnd cnd=Cnd.where("1","=","1");
		cnd.desc("UPTIME");
		return basicDao.search(MessageInfo.class, cnd);
	}

	public boolean deleteMessage(String id) {
		Cnd cnd=Cnd.where("Id","=",id);
		return basicDao.delete(MessageInfo.class, cnd);
	}

	public boolean updateMessageStatus(String id, String flag) {
		Cnd cnd=Cnd.where("Id","=",id);
		MessageInfo messageInfo=basicDao.findByCondition(MessageInfo.class, cnd);
		messageInfo.setId(messageInfo.getId());
		messageInfo.setTitle(messageInfo.getTitle());
		messageInfo.setContent(messageInfo.getContent());
		messageInfo.setStatus(flag);
		messageInfo.setUptime(new Date());
		return basicDao.update(messageInfo);
	}


}
